import sys
import matplotlib.pyplot as plt
import get_metric
# plt.rcParams['font.sans-serif'] = ['Times New Roman']
plt.rcParams.update({"font.size":20})


if __name__ == "__main__":
    file_name = sys.argv[1]
    SRC = sys.argv[2]
    DST = sys.argv[3]
    filename_a = sys.argv[4]
    filename_b = sys.argv[5]


    time_list, cwnd_list = get_metric.get_packets_in_flight(file_name, SRC, DST, None)
    # time_list, cwnd_list = get_metric.process_flows(file_name, SRC, DST)

    time_list = [t - time_list[0] for t in time_list]
    # loss_time_list = get_metric.get_loss_list(file_name)

    # wireless_loss_time_list = get_metric.get_loss_list_from_two_tcpdump(filename_a, filename_b, SRC, DST)
    # wireless_loss_time_list = [i for i in wireless_loss_time_list if i < 25]
    
    # wired_loss_time_list = get_metric.get_loss_list_from_two_tcpdump(file_name, filename_a, SRC, DST)
    
    plt.figure(figsize=(6, 3))
    plt.plot(time_list, cwnd_list)
    
    # for loss_time in wireless_loss_time_list:
    #     if loss_time <= time_list[-1]:
    #         plt.axvline(loss_time, color = 'r')

    # for loss_time in wired_loss_time_list:
    #     if loss_time <= time_list[-1]:
    #         plt.axvline(loss_time, color = 'green')

    plt.xlim(0,25)

    y_ub = sys.argv[6]
    if y_ub == '0' or None:
        plt.ylim(0,)
    else:
        plt.ylim(0, float(y_ub))

    plt.xlabel('Time')
    plt.ylabel('PIF (MBytes)')
    plt.grid()
    plt.tight_layout()
    py_file_name = __file__.split('/')[-1].split('.')[0]
    plt.savefig(f"{py_file_name}+{file_name}.png", bbox_inches='tight')
    plt.show()

    # Zoom in
    plt.figure(figsize=(6, 3))
    plt.plot(time_list, cwnd_list)
    
    # for loss_time in wired_loss_time_list:
    #     if loss_time <= time_list[-1]:
    #         plt.axvline(loss_time, color = 'green')
    
    # for loss_time in wireless_loss_time_list:
    #     if loss_time <= time_list[-1]:
    #         plt.axvline(loss_time, color = 'r')

    # plt.xlim(wireless_loss_time_list[0] - 1, wireless_loss_time_list[-1] + 1)

    y_ub = sys.argv[6]
    if y_ub == '0' or None:
        plt.ylim(0,)
    else:
        plt.ylim(0, float(y_ub))

    plt.xlabel('Time')
    plt.ylabel('PIF (MBytes)')
    plt.grid()
    plt.tight_layout()
    py_file_name = __file__.split('/')[-1].split('.')[0]
    plt.savefig(f"{py_file_name}+{file_name}_zoomin.png", bbox_inches='tight')
    # plt.show()
